package com.seadragon.app.ksr.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="CATEGORY", schema="STAGING")
public class Category {
	public void setParent(Category parent) {
		this.parent = parent;
	}

	@Id
	@Column(name = "ID")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CATEGORYIDSEQ")
	@SequenceGenerator(name = "CATEGORYIDSEQ", sequenceName = "STAGING.CATEGORY_ID_SEQ", schema="STAGING")	
	private long id;

	@Column(name="NAME", unique=true)
//	@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
	private String name;

	@Column(name="DESCRIPTION", length=1024)
	private String description;
	
	@Column(name="LEVEL")
//	@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
	private int level;
	
	@ManyToOne
	@JoinColumn(name="PARENT_ID")
    private Category parent;
	
//	@ContainedIn
	@OneToMany(mappedBy="parent", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	private List<Category> children;
	
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch=FetchType.EAGER)
	@JoinColumn(name="CATEGORY_ID")
	private Set<CategoryPriceCategorization> priceCategorizations;

	public Category(){
		this(null);
	}
	public Category(String name) {
		this.name = name;
		children = new ArrayList<Category>();
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public Category getParent() {
		return parent;
	}

	public void cello(Category parent) {
		this.parent = parent;
	}

	public List<Category> getChildren() {
		return children;
	}

	public void setChildren(List<Category> children) {
		this.children = children;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("Category: ");
		sb.append(this.getName());
		return sb.toString();
	}
	
	public Set<CategoryPriceCategorization> getPriceCategorizations() {
		return priceCategorizations;
	}
	public void setPriceCategorizations(Set<CategoryPriceCategorization> priceCategorizations) {
		this.priceCategorizations = priceCategorizations;
	}
	public boolean isLeaf(){
		return this.getChildren()== null || this.getChildren().size()==0;
	}
}
